﻿@page "/Wms/SaleOutWarehouse/Approve"
@using nERP.ViewModel.Wms.SaleOutWarehouseVMs
@using nERP.Model.Wms
@using System.ComponentModel.DataAnnotations
@inherits BasePage

<div>
    <ValidateForm @ref="vform" Model="@Model" OnValidSubmit="@Submit">
        <Row ItemsPerRow="ItemsPerRow.Three" RowType="RowType.Normal">
            <Display @bind-Value="@Model.Entity.SaleOutWarehouseCode" ShowLabel="true" />
            <Display @bind-Value="@Model.Entity.SaleId" ShowLabel="true" Lookup="AllSales" />
            <Display @bind-Value="@Model.Entity.CustomerId" ShowLabel="true" Lookup="AllCustomers" />
            <Display @bind-Value="@Model.Entity.MakeUserId" ShowLabel="true" Lookup="AllMakeUsers" />
            <Display @bind-Value="@Model.Entity.InvoicesDate" ShowLabel="true" FormatString="yyyy-MM-dd" />
            <Display @bind-Value="@Model.Entity.Status" ShowLabel="true" />
            <Display @bind-Value="@Model.Entity.Remark" ShowLabel="true" />
            <Display @bind-Value="@Model.Entity.CreateTime" ShowLabel="true" FormatString="yyyy-MM-dd HH:mm:ss" />
            <Display @bind-Value="@Model.Entity.UpdateTime" ShowLabel="true" FormatString="yyyy-MM-dd HH:mm:ss" />
            <Display @bind-Value="@Model.Entity.CreateBy" ShowLabel="true" />
            <Display @bind-Value="@Model.Entity.UpdateBy" ShowLabel="true" />
            <WTUploadImage @bind-Value="@Model.Entity.InvoicesFile" IsDisabled="true" thumb-width="128" />
        </Row>
        <Divider Icon="fa-fw fa-solid fa-bookmark" Text="@WtmBlazor.Localizer["_Model._SaleOutWarehouse._SaleOutWarehouseDetail_SaleOutWarehouse"]" Alignment="Alignment.Left" />


        <Table TItem="SaleOutWarehouseDetail"
               IsPagination="false"
               IsStriped="true" IsBordered="true" IsMultipleSelect="false" @bind-Items="@SaleOutWarehouseDetailSaleOutWarehouseList2" ShowFooter="true"
               ShowToolbar="false" ShowExtendButtons="false" ShowSkeleton="true">
            <TableColumns>
                <TableColumn @bind-Field="@context.Seq" Text="@WtmBlazor.Localizer["Page.序号"]" Readonly="true" />
                <TableColumn @bind-Field="@context.SaleOutWarehouseDetailCode" Text="@WtmBlazor.Localizer["Page.销售出库明细单号"]" Visible="false" />
                <TableColumn @bind-Field="@context.GoodsId" Text="@WtmBlazor.Localizer["Page.商品编号"]" Readonly="true" />
                <TableColumn @bind-Field="@context.GoodsName" Text="@WtmBlazor.Localizer["Page.商品名称"]" Readonly="true" />
                <TableColumn @bind-Field="@context.GoodsSpec" Text="@WtmBlazor.Localizer["Page.商品规格"]" Readonly="true" />
                <TableColumn @bind-Field="@context.GoodsUnit" Text="@WtmBlazor.Localizer["Page.计量单位"]" Readonly="true" />
                <TableColumn @bind-Field="@context.WarehouseId" Lookup="detailAllWarehouses" Text="@WtmBlazor.Localizer["Page.仓库"]" />
                <TableColumn @bind-Field="@context.SaleOutNumber" Text="@WtmBlazor.Localizer["Page.出库数量"]" FormatString="0.##" />
                <TableColumn @bind-Field="@context.Remark" Text="@WtmBlazor.Localizer["_Admin.Remark"]" />
            </TableColumns>
            <FooterTemplate>
                <tr style="background-color:gray; font-weight:600; text-align:center;">
                    <td colspan="6">合计：</td>
                    <td>@sumSaleOutNumber</td>
                </tr>
            </FooterTemplate>
        </Table>

        <Row>
            <Textarea @bind-Value="@Model.Remark" ShowLabel="true"></Textarea>
        </Row>
        <div class="modal-footer table-modal-footer">
            <Button ButtonType="ButtonType.Submit" IsAsync="true" OnClick="OnAgree" Text="@WtmBlazor.Localizer["Page.同意"]" />
            <Button ButtonType="ButtonType.Submit" IsAsync="true" OnClick="OnDeny" Color="Color.Danger" Text="@WtmBlazor.Localizer["Page.拒绝"]" />
            <Button OnClick="OnClose" Text="@WtmBlazor.Localizer["Sys.Close"]" />
        </div>

    </ValidateForm>

</div>



@code {
    [Parameter]
    public string id { get; set; }
    [Parameter]
    public string[] ids { get; set; }
    private SaleOutWarehouseVM Model = new SaleOutWarehouseVM();
    private ValidateForm vform { get; set; }
    private string sumSaleOutNumber { get; set; }
    public void OnClose()
    {
        CloseDialog();
    }
    private async Task Submit(EditContext context)
    {

        Model.Entity.SaleOutWarehouseDetail_SaleOutWarehouse = SaleOutWarehouseDetailSaleOutWarehouseList2.ToList();

        await PostsForm(vform, "/api/Wms/SaleOutWarehouse/Approve", (s) => "Sys.OprationSuccess");
    }
    public void OnAgree()
    {
        Model.ActionName = "同意";
        Model.Entity.Status = nERP.Model.StatusTypeEnum.Reviewed;
        Model.Entity.SaleOutWarehouseDetail_SaleOutWarehouse.ForEach(z0 => z0.Status = nERP.Model.dStatusTypeEnum.Reviewed);
    }
    public void OnDeny()
    {
        Model.ActionName = "拒绝";
    }


    private List<SelectedItem> AllSales = new List<SelectedItem>();
    private List<SelectedItem> AllCustomers = new List<SelectedItem>();
    private List<SelectedItem> AllMakeUsers = new List<SelectedItem>();
    private IEnumerable<SaleOutWarehouseDetail> SaleOutWarehouseDetailSaleOutWarehouseList2 { get; set; } = new List<SaleOutWarehouseDetail>();
    private List<SelectedItem> detailAllWarehouses = new List<SelectedItem>();
    protected override async Task OnInitializedAsync()
    {


        AllSales = await WtmBlazor.Api.CallItemsApi("/api/Wms/SaleOutWarehouse/GetSales");
        AllSales.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });
        AllCustomers = await WtmBlazor.Api.CallItemsApi("/api/Wms/SaleOutWarehouse/GetCustomers");
        AllCustomers.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });
        AllMakeUsers = await WtmBlazor.Api.CallItemsApi("/api/Wms/SaleOutWarehouse/GetFrameworkUsers");
        AllMakeUsers.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });
        if (id != "")
        {
            var rv = await WtmBlazor.Api.CallAPI<SaleOutWarehouseVM>($"/api/Wms/SaleOutWarehouse/{id}");
            Model = rv.Data;
        }
        SaleOutWarehouseDetailSaleOutWarehouseList2 = Model.Entity.SaleOutWarehouseDetail_SaleOutWarehouse;
        detailAllWarehouses = await WtmBlazor.Api.CallItemsApi("/api/Wms/SaleOutWarehouseDetail/GetWarehouses");
        detailAllWarehouses.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });

        var Items = Model.Entity.SaleOutWarehouseDetail_SaleOutWarehouse;
        sumSaleOutNumber = Items.Any() ? Items.Sum(x => x.SaleOutNumber)?.ToString("#0.####") : "0";
        await base.OnInitializedAsync();
    }
}
